# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=directxtk
_rn=DirectXTK
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.9.0
_tag=oct2024
pkgrel=1
pkgdesc="DirectX Tool Kit for DirectX 11 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://go.microsoft.com/fwlink/?LinkId=248929'
msys2_repository_url="https://github.com/microsoft/DirectXTK"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-directxmath"
             "${MINGW_PACKAGE_PREFIX}-efxc2")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("https://github.com/microsoft/DirectXTK/archive/${_tag}/${_realname}-${pkgver}.tar.gz"
        0001-directxtk-remove-mingw-workaround.patch)
sha256sums=('3809d151be675bf18aeeff22494ebc6dc8de882881f4ee86a15afdbafc94b7c1'
            'aab4d92f86afa117aaf323c7c4c87eaa30022565f2ca334892bf7b385bfabb20')

prepare() {
  cd "${_rn}-${_tag}"
  patch -p1 -i "${srcdir}/0001-directxtk-remove-mingw-workaround.patch"
}

build() {
  cd "${srcdir}/${_rn}-${_tag}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${CARCH}" == "x86_64" ]; then
    extra_config+=("-DDIRECTX_ARCH=x64")
  fi
  if [ "${CARCH}" == "aarch64" ]; then
    extra_config+=("-DDIRECTX_ARCH=arm64")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_TOOLS=ON \
      ../${_rn}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${_tag}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
